package com.example.lotterysystem.dao.mapper;

import com.example.lotterysystem.dao.dataobject.ErrorMessageDO;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ErrorMessageMapper {
    @Insert("<script>" +
            " insert into error_message " +
            " (message_id, activity_id, prize_id, winner_id, winner_name, winning_time)" +
            " values <foreach collection = 'items' item = 'item' index = 'index' separator=','>" +
            " (#{item.messageId}, #{item.activityId}, #{item.prizeId}," +
            " #{item.winnerId}, #{item.winnerName}, #{item.winningTime})" +
            " </foreach>" +
            " </script>")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int batchInsert(@Param("items") List<ErrorMessageDO> errorMessageDOList);

    @Select("select count(1) from error_message where processed = 0")
    int countProcessFalse();

    @Select("select * from error_message where processed = 0")
    List<ErrorMessageDO> selectProcessedFalse();

    @Update("<script>" +
            " update error_message " +
            " set processed = 1" +
            " where id in (" +
            " <foreach collection = 'items' item = 'item' index = 'index' separator=','>" +
            " #{item.id}" +
            " </foreach>)" +
            " </script>")
    @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
    int batchUpdate(@Param("items") List<ErrorMessageDO> errorMessageDOS);
}
